//get방식일경우 파라미터. 스크립트에서 바로 처리하기편하도록
var PARAM = {};
(function(window){
	var PARAM = window.PARAM;
	var queryString = window.location.search;
	if(!gfn_isEmpty(queryString)){
		queryString = queryString.replace("?","");
		var queryArr = queryString.split("&");
		$.each(queryArr, function(k, v){
			var tmp = v.split('=');
			var key = tmp[0];
			var val = tmp[1];
			PARAM[key] = val;
		});
	}
})(window);

//����Ŭ�� nvl2�Լ�(nvl������ε� ��밡��)
function gfn_nvl2(nullObj, trueObj, falseObj){
	if(gfn_isEmpty(nullObj))
		return trueObj;
	else{
		if(typeof falseObj == 'undefined' || falseObj == null){
			return nullObj;
		}else{
			return falseObj;
		}
	}
};

//object empty ���� üũ(undefined, null, '', �迭 Ȥ�� json�� ���̰� 0) �̸� true�� ��ȯ�Ѵ�.
function gfn_isEmpty( obj ) {
    var isBlack = function(string){
    	return (!string || $.trim(string) === "");
    };

	if(typeof obj == 'string'){
    	return isBlack(obj);
	}else if(typeof obj == 'number' || typeof obj == 'boolean' || obj instanceof Number){
		var tmp = obj.toString();
		return isBlack(tmp);
	}else if(typeof obj == 'function'){
		return false;
	}else if( obj instanceof Array ){	//�迭�� �� �迭���� ���� üũ�Ͽ� ����̸� empty��.
		if( obj.length < 1 )
			return true;
		else{
			var rtn = true;
			$.each( obj, function(i, v){
				if( '' != $.trim(v) )
					rtn = false;
			} );

			return rtn;
		}
    }else{
    	return $.isEmptyObject(obj);
    }
};


//날짜를 빼거나 더하기
function date_add(sDate, nDays) {
    var yy = parseInt(sDate.substr(0, 4), 10);
    var mm = parseInt(sDate.substr(5, 2), 10);
    var dd = parseInt(sDate.substr(8), 10);
 
    d = new Date(yy, mm - 1, dd + nDays);
 
    yy = d.getFullYear();
    mm = d.getMonth() + 1; mm = (mm < 10) ? '0' + mm : mm;
    dd = d.getDate(); dd = (dd < 10) ? '0' + dd : dd;
 
    return '' + yy + '-' +  mm  + '-' + dd;
}
//현재의 날짜를 반환한다.
function gfn_getToday(bool) {
	var date = new Date();
	var today = date.getFullYear()+''+(date.getMonth()+1)+''+date.getDate();
	var strToday = today;
	
	var y = strToday.substring(0, 4);
	var m = strToday.substring(4, 6);
	var d = strToday.substring(6, 8);
	var sReturn = null;

	if(bool)
		sReturn =   y + "-" + m.ex_lpad(2, 0) + "-" + d.ex_lpad(2, 0);
	else
		sReturn =   y + m.ex_lpad(2, 0) + d.ex_lpad(2, 0);

	return sReturn;
}


//HTML 템플릿 매니져
var TemplateManager = {
		tmplCache : {},
		htmlBaseURL : "/templete/",
		tmplHtml : function(id, vars){
			var obj = TemplateManager.tmpl(id, vars);
			var div = $("<div/>").append(obj);

			return div.html();
		},
		tmplXML : function(id, vars){
			var textHtml = this.bindVariable(this.getTemplate(id), vars);
			return textHtml;
		},
		getTemplate : function(id){
			var html;
			var url = TemplateManager.htmlBaseURL + id + ".html";

			html = $.ajax({url : url, async:false}).responseText;

			return html;
		},
		bindVariable : function(html, vars){
			var rtnHtml = html;
			if(!gfn_isEmpty(vars)){
				for(var key in vars){
					rtnHtml = rtnHtml.ex_replaceAll( "{{" + key + "}}", vars[key]);
				}
			}

			return rtnHtml;
		}
};

function replaceAll(str,orgStr,repStr){
    return str.split(orgStr).join(repStr);
}

function decodeHTMLEntities(text) {
    var entities = [
        ['quot', '"'],
        ['apos', '\''],
        ['amp', '&'],
        ['lt', '<'],
        ['gt', '>']
    ];

    for (var i = 0, max = entities.length; i < max; ++i) 
        text = text.replace(new RegExp('&'+entities[i][0]+';', 'g'), entities[i][1]);

    return text;
}
